home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 317 / asmsrc / obstack.h < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  12.3 KB  |  285 lines

  1. /* obstack.h - object stack macros
  2.    Copyright (c) 1986 Free Software Foundation, Inc.
  3.  
  4.                NO WARRANTY
  5.  
  6.   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  7. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  8. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  9. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  10. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  11. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  13. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  14. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  15. CORRECTION.
  16.  
  17.  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  18. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  19. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  20. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  21. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  22. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  23. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  24. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  25. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  26. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  27.  
  28.         GENERAL PUBLIC LICENSE TO COPY
  29.  
  30.   1. You may copy and distribute verbatim copies of this source file
  31. as you receive it, in any medium, provided that you conspicuously and
  32. appropriately publish on each copy a valid copyright notice "Copyright
  33. (C) 1986 Free Software Foundation, Inc."; and include following the
  34. copyright notice a verbatim copy of the above disclaimer of warranty
  35. and of this License.
  36.  
  37.   2. You may modify your copy or copies of this source file or
  38. any portion of it, and copy and distribute such modifications under
  39. the terms of Paragraph 1 above, provided that you also do the following:
  40.  
  41.     a) cause the modified files to carry prominent notices stating
  42.     that you changed the files and the date of any change; and
  43.  
  44.     b) cause the whole of any work that you distribute or publish,
  45.     that in whole or in part contains or is a derivative of this
  46.     program or any part thereof, to be freely distributed
  47.     and licensed to all third parties on terms identical to those
  48.     contained in this License Agreement (except that you may choose
  49.     to grant more extensive warranty protection to third parties,
  50.     at your option).
  51.  
  52.   3. You may copy and distribute this program or any portion of it in
  53. compiled, executable or object code form under the terms of Paragraphs
  54. 1 and 2 above provided that you do the following:
  55.  
  56.     a) cause each such copy to be accompanied by the
  57.     corresponding machine-readable source code, which must
  58.     be distributed under the terms of Paragraphs 1 and 2 above; or,
  59.  
  60.     b) cause each such copy to be accompanied by a
  61.     written offer, with no time limit, to give any third party
  62.     free (except for a nominal shipping charge) a machine readable
  63.     copy of the corresponding source code, to be distributed
  64.     under the terms of Paragraphs 1 and 2 above; or,
  65.  
  66.     c) in the case of a recipient of this program in compiled, executable
  67.     or object code form (without the corresponding source code) you
  68.     shall cause copies you distribute to be accompanied by a copy
  69.     of the written offer of source code which you received along
  70.     with the copy you received.
  71.  
  72.   4. You may not copy, sublicense, distribute or transfer this program
  73. except as expressly provided under this License Agreement.  Any attempt
  74. otherwise to copy, sublicense, distribute or transfer this program is void and
  75. your rights to use the program under this License agreement shall be
  76. automatically terminated.  However, parties who have received computer
  77. software programs from you with this License Agreement will not have
  78. their licenses terminated so long as such parties remain in full compliance.
  79. */
  80.  
  81. /* Summary:
  82.  
  83. All the apparent functions defined here are macros. The idea
  84. is that you would use these pre-tested macros to solve a
  85. very specific set of problems, and they would run fast.
  86. Caution: no side-effects in arguments please!! They may be
  87. evaluated MANY times!!
  88.  
  89. These macros operate a stack of objects.  Each object starts life
  90. small, and may grow to maturity.  (Consider building a word syllable
  91. by syllable.)  An object can move while it is growing.  Once it has
  92. been "finished" it never changes address again.  So the "top of the
  93. stack" is typically an immature growing object, while the rest of the
  94. stack is of mature, fixed size and fixed address objects.
  95.  
  96. These routines grab large chunks of memory, using a function you
  97. supply, called `obstack_chunk_alloc'.  On occasion, they free chunks,
  98. by calling `obstack_chunk_free'.  You must define them and declare
  99. them before using any obstack macros.
  100.  
  101. Each independent stack is represented by a `struct obstack'.
  102. Each of the obstack macros expects a pointer to such a structure
  103. as the first argument.
  104.  
  105. One motivation for this package is the problem of growing char strings
  106. in symbol tables.  Unless you are "facist pig with a read-only mind"
  107. [Gosper's immortal quote from HAKMEM item 154, out of context] you
  108. would not like to put any arbitrary upper limit on the length of your
  109. symbols.
  110.  
  111. In practice this often means you will build many short symbols and a
  112. few long symbols.  At the time you are reading a symbol you don't know
  113. how long it is.  One traditional method is to read a symbol into a
  114. buffer, realloc()ating the buffer every time you try to read a symbol
  115. that is longer than the buffer.  This is beaut, but you still will
  116. want to copy the symbol from the buffer to a more permanent
  117. symbol-table entry say about half the time.
  118.  
  119. With obstacks, you can work differently.  Use one obstack for all symbol
  120. names.  As you read a symbol, grow the name in the obstack gradually.
  121. When the name is complete, finalize it.  Then, if the symbol exists already,
  122. free the newly read name.
  123.  
  124. The way we do this is to take a large chunk, allocating memory from
  125. low addresses.  When you want to build a aymbol in the chunk you just
  126. add chars above the current "high water mark" in the chunk.  When you
  127. have finished adding chars, because you got to the end of the symbol,
  128. you know how long the chars are, and you can create a new object.
  129. Mostly the chars will not burst over the highest address of the chunk,
  130. because you would typically expect a chunk to be (say) 100 times as
  131. long as an average object.
  132.  
  133. In case that isn't clear, when we have enough chars to make up
  134. the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
  135. so we just point to it where it lies.  No moving of chars is
  136. needed and this is the second win: potentially long strings need
  137. never be explicitly shuffled. Once an object is formed, it does not
  138. change its address during its lifetime.
  139.  
  140. When the chars burst over a chunk boundary, we allocate a larger
  141. chunk, and then copy the partly formed object from the end of the old
  142. chunk to the beggining of the new larger chunk.  We then carry on
  143. accreting characters to the end of the object as we normaly would.
  144.  
  145. A special macro is provided to add a single char at a time to a
  146. growing object.  This allows the use of register variables, which
  147. break the ordinary 'growth' macro.
  148.  
  149. Summary:
  150.     We allocate large chunks.
  151.     We carve out one object at a time from the current chunk.
  152.     Once carved, an object never moves.
  153.     We are free to append data of any size to the currently
  154.       growing object.
  155.     Exactly one object is growing in an obstack at any one time.
  156.     You can run one obstack per control block.
  157.     You may have as many control blocks as you dare.
  158.     Because of the way we do it, you can `unwind' a obstack
  159.       back to a previous state. (You may remove objects much
  160.       as you would with a stack.)
  161. */
  162.  
  163. #ifndef obstackH
  164. #define obstackH
  165.                 /* these #defines keep it brief */
  166. #define _Ll struct obstack_chunk
  167. #define _LL (8)            /* _L length in chars */
  168.  
  169. struct obstack_chunk        /* Lives at front of each chunk. */
  170. {
  171.   char  *obstack_l_limit;    /* 1 past end of this chunk */
  172.   _Ll    *obstack_l_prev;    /* address of prior chunk or NULL */
  173.   char    obstack_l_0[4];        /* objects begin here */
  174. };
  175.  
  176. #if 0
  177. This function, called like malloc but not returning on failure,
  178. must return a chunk of the size given to it as argument,
  179. aligned on a boundary of 2**OBSTACK_LOG_DEFAULT_ALIGNMENT bytes.
  180.  
  181. struct obstack_chunk * obstack_chunk_alloc();
  182. #endif /* 0 */
  183.  
  184. struct obstack        /* control current object in current chunk */
  185. {
  186.   long    chunk_size;        /* preferred size to allocate chunks in */
  187.   _Ll*    chunk;            /* address of current struct obstack_chunk */
  188.   char    *object_base;        /* address of object we are building */
  189.   char    *next_free;        /* where to add next char to current object */
  190.   char    *chunk_limit;        /* address of char after current chunk */
  191.   int    temp;            /* Temporary for some macros.  */
  192.   int   alignment_mask;        /* Mask of alignment for each object. */
  193. };
  194.  
  195. void _obstack_newchunk();
  196. void _obstack_begin();
  197.  
  198. /* Pointer to beginning of object being allocated or to be allocated next.
  199.    Note that this might not be the final address of the object
  200.    because a new chunk might be needed to hold the final size.  */
  201.  
  202. #define obstack_base(h) ((h)->object_base)
  203.  
  204. /* Pointer to next byte not yet allocated in current chunk.  */
  205.  
  206. #define obstack_next_free(h)    ((h)->next_free)
  207.  
  208. /* Size of object currently growing */
  209.  
  210. #define obstack_object_size(h)  ((h)->next_free - (h)->object_base)
  211.  
  212. /* Mask specifying low bits that should be clear in address of an object.  */
  213.  
  214. #define obstack_alignment_mask(h) ((h)->alignment_mask)
  215.  
  216. #define obstack_init(h) obstack_begin (h, 4096 - 4 - _LL)
  217.  
  218. #define obstack_begin(h,try_length)                    \
  219. ((h)->chunk_size = (try_length) + (_LL),                \
  220.  (h)->alignment_mask = ((1 << 2) - 1),                    \
  221.  _obstack_begin ((h), obstack_chunk_alloc))
  222.  
  223. #define obstack_grow(h,where,length)                    \
  224. ( (h)->temp = (length),                            \
  225.   (((h)->next_free + (h)->temp > (h)->chunk_limit)            \
  226.    ? (_obstack_newchunk ((h), obstack_chunk_alloc, (h)->temp),0) : 0),    \
  227.   bcopy (where, (h)->next_free, (h)->temp),                \
  228.   (h)->next_free += (h)->temp)
  229.  
  230. #define obstack_grow0(h,where,length)                    \
  231. ( (h)->temp = (length),                            \
  232.   (((h)->next_free + (h)->temp + 1 > (h)->chunk_limit)            \
  233.    ? (_obstack_newchunk ((h), obstack_chunk_alloc, (h)->temp + 1),0) : 0),    \
  234.   bcopy (where, (h)->next_free, (h)->temp),                \
  235.   (h)->next_free += (h)->temp,                        \
  236.   *((h)->next_free)++ = 0)
  237.  
  238. #define obstack_1grow(h,datum)                        \
  239. ( (((h)->next_free + 1 > (h)->chunk_limit)                \
  240.    ? (_obstack_newchunk ((h), obstack_chunk_alloc, 1),0) : 0),        \
  241.   *((h)->next_free)++ = (datum))
  242.  
  243. #define obstack_blank(h,length)                        \
  244. ( (h)->temp = (length),                            \
  245.   (((h)->next_free + (h)->temp > (h)->chunk_limit)            \
  246.    ? (_obstack_newchunk ((h), obstack_chunk_alloc, (h)->temp),0) :0),    \
  247.   (h)->next_free += (h)->temp)
  248.  
  249. #define obstack_alloc(h,length)                        \
  250.  (obstack_blank ((h), (length)), obstack_finish (h))
  251.  
  252. #define obstack_copy(h,where,length)                    \
  253.  (obstack_grow ((h), (where), (length)), obstack_finish (h))
  254.  
  255. #define obstack_copy0(h,where,length)                    \
  256.  (obstack_grow0 ((h), (where), (length)), obstack_finish (h))
  257.  
  258. #define obstack_room(h) ((long unsigned int)                \
  259.  ((h)->chunk_limit - (h)->next_free))
  260.  
  261. #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = achar)
  262.  
  263. #define obstack_blank_fast(h,n) ((h)->next_free += (n))
  264.  
  265. #define obstack_finish(h)                          \
  266.  ((h)->temp = (int) (h)->object_base,                    \
  267.   (h)->next_free                            \
  268.     = (char*)((int)((h)->next_free+(h)->alignment_mask)            \
  269.           & ~ ((h)->alignment_mask)),                \
  270.   (((h)->next_free - (char *)(h)->chunk                    \
  271.     > (h)->chunk_limit - (char *)(h)->chunk)                \
  272.    ? (h)->next_free = (h)->chunk_limit : 0),                \
  273.   (h)->object_base = (h)->next_free,                    \
  274.   (char *) (h)->temp)
  275.  
  276. #define obstack_free(h,obj)                        \
  277. (((h)->temp = (char *)(obj) - (char *) (h)->chunk),            \
  278.  (((h)->temp >= 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\
  279.   ? (int) ((h)->next_free = (h)->object_base                \
  280.        = (h)->temp + (char *) (h)->chunk)                \
  281.   : (int) _obstack_free ((h), obstack_chunk_free,            \
  282.              (h)->temp + (char *) (h)->chunk)))
  283.  
  284. #endif                /* #ifndef obstackH */
  285.